package com.sayimo.maker.mall.goods.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.sayimo.framework.exception.DaoException;
import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.mall.goods.dao.GoodsCollectionDao;
import com.sayimo.maker.mall.goods.service.GoodsCollectionService;
import com.sayimo.maker.mall.goods.vo.GoodsCollection;


@Service(value="goodsCollectionService")
public class GoodsCollectionServiceImpl implements GoodsCollectionService{
	
	private static Log logger = LogFactory.getLog(GoodsCollectionServiceImpl.class);
	
	@Resource(name="goodsCollectionDao")
	private GoodsCollectionDao goodsCollectionDao;
	/**
	 * 
	* @Title: addGoodsCollection 
	* @Author: 田超
	* @Date:日期：2016年9月12日  时间：下午6:40:53
	* @Description: 添加收藏
	* @param request
	* @return
	* @return Map<String,Object>
	 * @throws ServiceException 
	* @Version 1.0.0
	 */
	public Map<String, Object> addGoodsCollection(HttpServletRequest request) throws ServiceException {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		GoodsCollection goodsCollection = new GoodsCollection();
		goodsCollection.setCustomerId((Integer) request.getAttribute("customerId"));
		goodsCollection.setGoodsId(Integer.parseInt(request.getParameter("goodsId")));
		goodsCollection.setNormsValueId(Integer.parseInt(request.getParameter("normsValueId")));
		goodsCollection.setType(Integer.parseInt(request.getParameter("type")));
		goodsCollection.setIdentifier(request.getParameter("identifier"));
		goodsCollection.setIsactivity(Integer.parseInt(request.getParameter("isactivity")));
		goodsCollection.setSeckillTimesId(Integer.parseInt(request.getParameter("seckillTimesId")));
		try {
			Integer count = goodsCollectionDao.insertGoodsCollection(goodsCollection);
			if(count > 0){
				resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				resultMap.put(Constant.DATA, Constant.SUCCESS);
			}else{
				resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
				resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
			}
		} catch (DaoException e) {
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
			logger.error(e);
			throw new ServiceException(e);
		}
		return resultMap;
	}

	/**
	 * 
	* @Title: queryGoodsCollection 
	* @Author: 田超
	* @Date:日期：2016年9月12日  时间：下午6:41:50
	* @Description: 分页查询收藏
	* @param request
	* @return
	* @return Map<String,Object>
	 * @throws ServiceException 
	* @Version 1.0.0
	 */
	public Map<String, Object> queryGoodsCollection(Integer customerId,Integer pageNow,Integer pageSize,Integer type) throws ServiceException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		List<GoodsCollection> goodsColletion = null;
		if(customerId != null){
			paramMap.put("customerId", customerId);
		}
		if(pageNow != null && pageNow == 0){
			paramMap.put("pageNow", 0);
		}else if(pageNow != null && pageNow > 0){
			paramMap.put("pageNow", (pageNow-1)*pageSize);
		}
		if(pageSize != null){
			paramMap.put("pageSize", pageSize);
		}
		if(type != null && type == 1){
			try {
				paramMap.put("type", type);
				goodsColletion = goodsCollectionDao.queryGoodsCollection(paramMap);
			} catch (DaoException e) {
				resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
				resultMap.put(Constant.ERROR, Constant.SYSTEM_ERROR);
				logger.error(e);
				throw new ServiceException(e);
			}
		}else if(type != null && type == 2){
			try {
				paramMap.put("type", type);
				goodsColletion = goodsCollectionDao.queryPreGoodsCollection(paramMap);
			} catch (DaoException e) {
				resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
				resultMap.put(Constant.ERROR, Constant.SYSTEM_ERROR);
				logger.error(e);
				throw new ServiceException(e);
			}
		}
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("goodscollectionList", goodsColletion);
		resultMap.put(Constant.DATA, dataMap);
		resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
		return resultMap;
	}

	/**
	 * 
	* @Title: deleteGoodsCollection 
	* @Author: 田超
	* @Date:日期：2016年9月12日  时间：下午6:43:02
	* @Description: 删除收藏
	* @param request
	* @return
	* @return Map<String,Object>
	 * @throws ServiceException 
	* @Version 1.0.0
	 */
	public Map<String, Object> deleteGoodsCollection(HttpServletRequest request) throws ServiceException {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		Integer customerId = (Integer) request.getAttribute("customerId");
		Integer goodsId = Integer.parseInt(request.getParameter("goodsId"));
		String identifier = request.getParameter("identifier");
		String seckillTimesId = request.getParameter("seckillTimesId");
		if(identifier != null && !identifier.equals("")){
			paramMap.put("identifier", identifier);
		}else {
			paramMap.put("identifier", "0");
		}
		if(seckillTimesId != null && !seckillTimesId.equals("")){
			paramMap.put("seckillTimesId",Integer.parseInt(seckillTimesId));
		}else {
			paramMap.put("seckillTimesId",0);
		}
		paramMap.put("customerId", customerId);
		paramMap.put("goodsId", goodsId);
		try {
			Integer count = goodsCollectionDao.deleteGoodsCollection(paramMap);
			if(count > 0){
				resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				resultMap.put(Constant.DATA, Constant.SUCCESS);
			}else{
				resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
				resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
			}
		} catch (DaoException e) {
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
			logger.error(e);
			throw new ServiceException(e);
		}
		return resultMap;
	}

}
